﻿namespace CommonData.Data.Core.SQLCore
{
    using CommonData.Data.Core;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SqlProvider : IDbProvider, IDisposable
    {
        private IDbDataAdapter adapter = null;
        private IDbCommand command = null;
        private IDbConnection connection = null;
        private string connectionString = @"server=QINGYUAN\SQLEXPRESS;database=StuDB;Integrated Security=true";
        private IDbTransaction transaction = null;

        public void BeginTransaction()
        {
            this.transaction = this.connection.BeginTransaction();
            this.command.Transaction = this.transaction;
        }

        public void Close()
        {
            if (this.connection != null)
            {
                this.connection.Close();
            }
        }

        public void Commit()
        {
            if (this.transaction != null)
            {
                this.transaction.Commit();
                this.command.Transaction = null;
                this.transaction.Dispose();
            }
        }

        public IDbProvider CreateInstance()
        {
            return new SqlProvider();
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public void Open()
        {
            if (this.connection != null)
            {
                this.connection.Open();
            }
        }

        public void RollBack()
        {
            if (this.transaction != null)
            {
                this.transaction.Rollback();
                this.command.Transaction = null;
                this.transaction.Dispose();
            }
        }

        public IDbDataAdapter Adapter
        {
            get
            {
                if (this.adapter == null)
                {
                    this.adapter = new SqlDataAdapter(this.command as SqlCommand);
                }
                return this.adapter;
            }
            set
            {
                this.adapter = value;
            }
        }

        public IDbCommand Command
        {
            get
            {
                if (this.command == null)
                {
                    this.command = new SqlCommand();
                    this.command.Connection = this.connection;
                }
                return this.command;
            }
            set
            {
                this.command = value;
            }
        }

        public IDbConnection Connection
        {
            get
            {
                if (this.connection == null)
                {
                    this.connection = new SqlConnection(this.connectionString);
                }
                return this.connection;
            }
            set
            {
                this.connection = value;
            }
        }

        public string ConnectionString
        {
            get
            {
                if (this.connectionString == null)
                {
                    this.connectionString = @"serverQINGYUAN\SQLEXPRESS;database=StuDB;Integrated Security=true";
                }
                return this.connectionString;
            }
            set
            {
                this.connectionString = value;
            }
        }
    }
}

